//#include "stdafx.h"
#include "ProcessList.h"


ProcessList::ProcessList()
{

}

//bool ProcessList::isValidDate(Date date, Date currentDate)
//{
//	bool flag=true;
//	if(date.getDay()<1)
//		flag=false;
//	if(date.getMonth()<1 || date.getMonth()> 12)
//		flag=false;
//	if(date.getYear()<2011 || date.getYear() > 2099)
//		flag=false;
//	int daysInMonths[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//	if(date.isLeap())
//		daysInMonths[2]+=1;
//	if(date.getDay()>daysInMonths[date.getMonth()])
//		flag=false;
//	if(date.compareDates(currentDate)>0)
//		flag=false;
//	return flag;
//}

//Date ProcessList:: setDateObject(int day, int month, int year)
//{
//	assert(day>0);
//	assert(month>0);
//	assert(year>0);
//	Date dateObjectToBeReturned;
//	dateObjectToBeReturned.setDay(day);
//	dateObjectToBeReturned.setMonth(month);
//	dateObjectToBeReturned.setYear(year);
//	return dateObjectToBeReturned;
//}
//
//Date ProcessList::dateStringToObject(string date, Date currentDate)
//{
//	Date convertedDateObject;
//	vector <string> tokenizedDate=tokenize(date);
//	vector<string> dateWithoutSlash;
//	int day=0;
//	int month=0;
//	int year=0;
//
//	if(tokenizedDate.size()==1)
//	{
//		if(equalsIgnoreCase(date, "today"))
//		{
//			day = currentDate.getDay();
//			month = currentDate.getMonth();
//			year = currentDate.getYear();
//		}
//
//		else if(equalsIgnoreCase(date, "tomorrow"))
//		{
//			Date nextDate=currentDate;
//			nextDate.incrementDays(1);
//			day = nextDate.getDay();
//			month = nextDate.getMonth();
//			year = nextDate.getYear();
//		}
//		
//		else if (hasSlashes(date))
//		{
//			dateWithoutSlash=removeSlash(date);
//			
//			if(dateWithoutSlash.size()==2)
//			{
//				day = atoi(dateWithoutSlash[0].c_str());
//				month = atoi(dateWithoutSlash[1].c_str());
//				year = currentDate.getYear();
//			}
//
//			else if(dateWithoutSlash.size()==3)
//			{
//				//assuming input is in dd/mm/yyyy format only
//				day=atoi(dateWithoutSlash[0].c_str());
//				month=atoi(dateWithoutSlash[1].c_str());
//				year= atoi(dateWithoutSlash[2].c_str());
//			}
//		}
//
//		else if (date.size()==8)
//		{
//			day=atoi(date.substr(0,2).c_str());
//			month=atoi(date.substr(2,2).c_str());
//			year= atoi(date.substr(4,4).c_str());
//		}
//		convertedDateObject = setDateObject(day, month, year);
//		if(convertedDateObject.compareDates(currentDate)>0 && day!=0)
//				convertedDateObject.setYear(currentDate.getYear()+1);
//	}
//
//	else if(tokenizedDate.size()>1 && tokenizedDate.size()<4)
//	{
//		//assuming input will be in the form of "September 29" or "29 September" or "29 September 2011" only
//		if(isMonth(tokenizedDate[0]))
//		{
//			day=atoi(tokenizedDate[1].c_str());
//			month=whichMonth(tokenizedDate[0]);
//			year = currentDate.getYear();
//		}
//		else if(isMonth(tokenizedDate[1]))
//		{
//			day=atoi(tokenizedDate[0].c_str());
//			month=whichMonth(tokenizedDate[1]);
//			year = currentDate.getYear();
//		}
//
//		if (tokenizedDate.size()==3)
//			year=atoi(tokenizedDate[2].c_str());
//
//		convertedDateObject=setDateObject(day, month, year);
//		if(convertedDateObject.compareDates(currentDate)>0 && day!=0 && month !=0)
//				convertedDateObject.setYear(currentDate.getYear()+1);
//
//	}
//	return convertedDateObject;
//}
//
//bool ProcessList::hasSlashes(string date)
//{
//	int pos=date.find('/');
//	int pos2=date.find('.');
//	if(pos>-1 || pos2>-1)
//		return true;
//	return false;
//}

//int ProcessList::findHyphen(string timeToBeTested)
//{
//	int pos=timeToBeTested.find('-');
//	if(pos>-1)
//		return pos;
//	return -1;
//}
//
//int ProcessList::findColonOrPeriod(string timeToBeTested)
//{
//	int pos=timeToBeTested.find(':');
//	int pos2=timeToBeTested.find('.');
//	int pos3=timeToBeTested.find(",!@#$%^&*(){}[]\|;/?><\`~");
//	if (pos3==-1)
//	{
//		if(pos>-1)
//			return pos;
//		else if (pos2>-1)
//			return pos2;
//	}
//	return -1;
//}
//
//int ProcessList::findAmPm(string timeToBeTested)
//{
//	timeToBeTested=toLower(timeToBeTested);
//	int pos=timeToBeTested.find("am");
//	int pos2=timeToBeTested.find("pm");
//	if(pos>-1)
//		return pos;
//	else if (pos2>-1)
//		return pos2;
//	return -1;
//}

string ProcessList::toLower(string am_pm)
{
	stringstream converted;
	for (int i=0; i<am_pm.size();i++)
	{
		if (isdigit(am_pm[i]))
			converted << am_pm[i];
		else
			converted << (char)tolower(am_pm[i]);
	}
	return converted.str();
}

//int ProcessList::whichMonth(string month)
//{
//	string months[]={"january","february","march","april","may","june","july","august","september","october","november","december"};
//	month=toLower(month);
//	int i;
//	for(i=0;i<12;i++)
//	{
//		if(months[i].compare(0,month.size(),month)==0)
//			break;
//	}
//		
//	return i+1;
//}
//
//bool ProcessList::isMonth(string month)
//{
//	string months[]={"january","february","march","april","may","june","july","august","september","october","november","december"};
//	month=toLower(month);
//	bool flag=false;
//
//	if(month.size()>=3)
//	{
//		for(int i=0;i<12;i++)
//		{
//			if(months[i].compare(0,month.size(),month)==0)
//			{
//				flag=true;
//				break;
//			}
//		}
//	}
//	return flag;
//}
//
////tokenize date
//vector<string> ProcessList::tokenize(string inputDate)
//{
//	stringstream strstr(inputDate);
//	vector<string> tokenizedDate;
//	string buffer;
//	while(strstr >> buffer)
//		tokenizedDate.push_back(buffer);
//	return tokenizedDate;
//}
//
//vector<string> ProcessList::removeSlash(string date)
//{
//	vector<string> dateWithoutSlash;
//	char *pch;
//	char *str;
//	str = new char [date.size()+1];
//	strcpy(str, date.c_str());
//
//	pch = strtok(str,"/.");
//	while(pch!=NULL)
//	{
//		dateWithoutSlash.push_back(pch);
//		pch=strtok(NULL,"/.");
//	}
//	return dateWithoutSlash;
//}

bool ProcessList::equalsIgnoreCase(string str1, string str2){
	
	if (str1.size() != str2.size()) 
		return false;
    for (string::const_iterator c1 = str1.begin(), c2 = str2.begin(); c1 != str1.end(); ++c1, ++c2){
        if (tolower(*c1) != tolower(*c2)) 
			return false;
    }
    return true;
}


//vector<string> ProcessList::tokenizeTime(string time)
//{
//	vector<string> tokenizedTime;
//	char *pch;
//	char *str;
//	str = new char [time.size()+1];
//	strcpy(str, time.c_str());
//	string temp;
//
//	pch = strtok(str,":. ");
//	while(pch!=NULL)
//	{
//		temp=pch;
//		if(temp.size()>2)
//		{
//			int index=findAmPm(temp);
//			if(index!=-1)
//			{
//				tokenizedTime.push_back(temp.substr(0,index));
//				tokenizedTime.push_back(temp.substr(index,2));
//			}
//			else
//				tokenizedTime.push_back(temp);
//		}
//		else
//			tokenizedTime.push_back(temp);
//		pch=strtok(NULL,":. ");
//	}
//	return tokenizedTime;
//}
//
//int ProcessList::hasAmPm(vector<string> tokenizedTime)
//{
//	for(int i=0;i<tokenizedTime.size();i++)
//	{
//		if(equalsIgnoreCase(tokenizedTime[i],"am") || equalsIgnoreCase(tokenizedTime[i],"pm"))
//			return i;
//	}
//	return -1;
//}
//
//bool ProcessList::isValidTime(Time time)
//{
//	bool flag=true;
//	if(time.getHour()<1 || time.getHour()>12)
//		flag=false;
//	if(time.getMinutes()<0 || time.getMinutes()>59)
//		flag=false;
//	if(!(equalsIgnoreCase(time.getAmPm(),"am") || equalsIgnoreCase(time.getAmPm(),"pm")))
//		flag=false;
//	return flag;
//}
//
//Time ProcessList::timeStringToObject(string time)
//{
//	Time temp;
//	vector<string> tokenizedTime=tokenizeTime(time);
//
//	if(tokenizedTime.size()==2)
//	{
//		if(hasAmPm(tokenizedTime)==1)
//		{
//			int hour=atoi(tokenizedTime[0].c_str());
//			string am_pm=tokenizedTime[1];
//
//			temp.setHour(hour);
//			temp.setAmPm(am_pm);
//		}
//	}
//	
//	else if(tokenizedTime.size()==3)
//	{
//		if(hasAmPm(tokenizedTime)==2)
//		{
//			int hour=atoi(tokenizedTime[0].c_str());
//			int minutes=atoi(tokenizedTime[1].c_str());
//			string am_pm=tokenizedTime[2];
//
//			temp.setHour(hour);
//			temp.setMinutes(minutes);
//			temp.setAmPm(am_pm);
//		}
//	}
//
//	return temp;
//
//}
//
//
//string ProcessList:: formatTime(Time time)
//{
//	int hour=time.getHour(), min=time.getMinutes();
//	string am_pm=time.getAmPm();
//	stringstream result;
//	
//	if (hour<10)
//		result <<'0';
//	result<<hour<<":";
//	
//	if (min<10)
//		result <<'0';
//	result<<min;
//	result << toLower(am_pm); //implement to lower function
//	
//	return result.str();
//}

bool ProcessList:: isValidAdd(struct Task currentTask)
{
	if (currentTask.name=="")
		return false;
	if (currentTask.date=="")
		return false;
	if (currentTask.time=="")
		return false;

	return true;
}

string ProcessList::trim(string text){
	
	string trimmedText;
	int startPos=text.find_first_not_of(' ');
	int endPos=text.find_last_not_of(' ');
	if(startPos==-1)
		trimmedText="";
	else
		trimmedText=text.substr(startPos,endPos-startPos+1);
	return trimmedText;
}

string ProcessList::generateId(Date eventDate)
{
	string eventId="";
	stringstream temp;
	Event tempEvent;
	int day=eventDate.getDay();
	int month=eventDate.getMonth();
	int year=eventDate.getYear();
	double newId = 0;
	string currentId="", prevId="";

	
	if (eventDate.getEventListLength() >0)
	{
		eventId = eventDate.getLargestId();//(tempEvent.getId());
		newId = atoi(eventId.substr(8,2).c_str() ) + 1;
		temp << eventId.substr(0,8).c_str();
		if (newId < 10)
			temp << '0';
		temp << newId;
	}
	else
	{
		if(day<10)
			temp << 0 << day;
		else temp<< day;
		
		if(month < 10)
			temp << 0 << month;
		else temp << month;
		
		temp << year;
		temp << "01";
	}
	eventId = temp.str();
	return eventId;
}

string ProcessList::getEventDetailsId(vector<Date> currentList,vector<Event> displayList, struct Task currentTask)
{
	if (currentTask.index!="")
	{
		int index = atoi(currentTask.index.c_str());
		if (displayList.size()>=index)
		{
			string id = displayList[index-1].getId();
			Date dateObj=currentList[0].dateStringToObject(id.substr(0,8));
			int dateIndex=currentList[0].dateDifference(dateObj);
		
			return currentList[dateIndex].getEventDetails(currentList[dateIndex].getIndex(id));
		}
		return "";
	}
	else 
	{
		stringstream details;
		
		details << currentTask.name;
		if (currentTask.location !="")
			details << " at " << currentTask.location;
		details << " at "<< currentTask.time;
		details << ", ";
		details << currentTask.date;
		details << endl;

		return details.str();
	}
}

void ProcessList::fillDates(vector<Date>& currentList, int index)
{
	Date temp=currentList[currentList.size()-1];
	Date temp1;
	for (int i = currentList.size(); i <=index; i++)
	{
		temp.incrementDays(1);
		temp1.setDay(temp.getDay());
		temp1.setMonth(temp.getMonth());
		temp1.setYear(temp.getYear());
		currentList.push_back(temp1);
	}
}

//bool ProcessList::checkOverallTime(string timeToCheck, Time time[])
//{
//	bool flag = false;
//	int hyphenPos = findHyphen(timeToCheck);
//	if (hyphenPos>-1)
//	{
//		time[0]=timeStringToObject(timeToCheck.substr(0,hyphenPos));
//		time[1]=timeStringToObject(timeToCheck.substr(hyphenPos+1));
//		if(isValidTime(time[0]) && isValidTime(time[1]))
//			flag=true;
//	}
//	else
//	{
//		time[0]=timeStringToObject(timeToCheck.substr(0,hyphenPos));
//		if(isValidTime(time[0]))
//			flag=true;
//	}
//	return flag;
//}

bool ProcessList::addEvent (vector<Date>& currentList,struct Task currentTask)//, string& statusMessage)
{
	bool flag = true;
	Date currentDate;
	currentDate=currentList[0];
	Time time[2];
	Time tempTime;
	bool isTime2Valid=true;
	stringstream convertedTime;
	
	if (!isValidAdd(currentTask))
		flag = false;
	else if (!tempTime.getStartEndTime(currentTask.time, time))
		flag = false;
	
			
	if (currentTask.deadline =="d" && time[1].getHour()!=0)
		flag = false;

	Date dateToAdd=currentDate.dateStringToObject(currentTask.date);
	if(currentDate.isValidDate(dateToAdd) && flag)
	{
		int index = currentList[0].dateDifference(dateToAdd);
	
		if (currentList.size()<=index)
			fillDates(currentList, index);

		convertedTime << time[0].formatTime();
		if(time[1].getHour()!=0)
			convertedTime << '-' << time[1].formatTime();


		string objUniqueId = currentTask.id;
		if (objUniqueId =="")
			objUniqueId= generateId(currentList[index]);

		if (currentTask.deadline !="d")
			currentTask.deadline = "se";

		if(currentTask.status=="")
			currentTask.status="nd";

		currentList[index].addEvent(objUniqueId,currentTask.name,convertedTime.str(), currentTask.location, time[0], time[1], currentTask.deadline, currentTask.status);
		//if (checkIfClash(currentList[index].getEventList(), time)) ----- to check for clashes, implemented through exceptions
			
		//statusMessage = currentList[index].getEventDetails(currentList[index].getIndex(objUniqueId));
		commandOrder.push("add");
		eventOrder.push(currentList[index].getEvent(currentList[index].getIndex(objUniqueId)));
	
		return true;
	}
	return false;
}

bool ProcessList::checkIfClash(vector<Event> eventList, Time time[2])
{
	bool flag = false;
	Time startTime = time[0];
	Time endTime = time[1];

	for (int i=0; i<eventList.size(); i++)
	{
		if (endTime.getAmPm()=="")
		{
			if (eventList[i].getStartTime().getHour() == time[0].getHour())
			{
				flag = true;
				break;
			}
		}
		else
		{
			if (eventList[i].getStartTime().getHour() >= time[0].getHour() && eventList[i].getStartTime().getHour() < time[1].getHour())
			{
				flag = true;
				break;
			}
		}
	}
	return flag;
}

bool ProcessList::deleteEvent (vector<Date>& currentList, vector<Event>& displayList ,struct Task currentTask)
{
	bool flag=true;
	int displayIndex, i;
	Event eventObj;
	
	displayIndex=atoi(currentTask.index.c_str());
	if(displayIndex<1||displayIndex>displayList.size())
	{
		flag=false;
	}

	else
	{
		string id = displayList[displayIndex-1].getId();
		string date=id.substr(0,8);
		Date dateObj=currentList[0].dateStringToObject(date);
		int dateIndex=currentList[0].dateDifference(dateObj);
		int indexToDelete=currentList[dateIndex].getIndex(id);
				
		if (indexToDelete!=-1)
		{
			eventObj=currentList[dateIndex].getEvent(indexToDelete);
			currentList[dateIndex].deleteEvent(indexToDelete);
			//statusMessage = currentList[dateIndex].getEventDetails(indexToDelete);
			commandOrder.push("delete");
			eventOrder.push(eventObj);
		}
		else
			flag=false;
	}
	return flag;
}

bool ProcessList::editEvent (vector<Date>& currentList, vector<Event>& displayList ,struct Task currentTask)
{
	Event eventObj;
	struct Task newTask;
	bool flag = true;
	int displayIndex, i;
	Time time[2];
	stringstream convertedTime;
	displayIndex=atoi(currentTask.index.c_str());
	if(displayIndex<1||displayIndex>displayList.size())
	{
		flag = false;
	}
	else
	{
		string id = displayList[displayIndex-1].getId();
		string date=id.substr(0,8);
		Date dateObj=currentList[0].dateStringToObject(date);
		int dateIndex=currentList[0].dateDifference(dateObj);
		int indexToEdit=currentList[dateIndex].getIndex(id);
		Time timeToEdit;
		Time tempTime;
		eventObj = displayList[displayIndex-1];
		if (indexToEdit!=-1)
		{
			if (currentTask.date!="")
			{
				//eventObj = displayList[displayIndex-1];
				deleteEvent(currentList, displayList, currentTask);
				eventOrder.pop();
				commandOrder.pop();

				newTask.name = eventObj.getName();
				newTask.location = eventObj.getLocation();
				newTask.time = eventObj.getTime ();
				newTask.status=eventObj.getEventStatus();
				newTask.date = currentTask.date;
				if (eventObj.getEventType()=="d")
					newTask.deadline = "d";
				

				if (!addEvent (currentList, newTask))
				{
					newTask.id=eventObj.getId();
					newTask.date=eventObj.getId().substr(0,8);
					addEvent(currentList, newTask);
					flag = false;
				}
				else
				{
					dateObj=currentList[0].dateStringToObject(currentTask.date);
					//statusMessage = currentList[dateIndex].getEventDetails(indexToEdit);
					dateIndex=currentList[0].dateDifference(dateObj);
					indexToEdit=currentList[dateIndex].getIndex(currentList[dateIndex].getLargestId());//ndex(current);
					eventObj=currentList[dateIndex].getEvent(indexToEdit);
				}
				eventOrder.pop();
				commandOrder.pop();

			}
	
			if (currentTask.time!="")
			{
				if (tempTime.getStartEndTime(currentTask.time, time))
				{
					//eventObj = displayList[displayIndex-1];
					if (eventObj.getEventType() =="d" && time[1].getHour() !=0)
							flag = false;
					
					else
					{
						convertedTime << time[0].formatTime();
						if(time[1].getHour()!=0)
							convertedTime << '-' << time[1].formatTime();
						currentTask.time = convertedTime.str();
					}
					//currentList[dateIndex].editEvent(indexToEdit,currentTask);
				}
				else
					flag = false;
			}
			
			if (flag)
			{
				eventOrder.push(eventObj);
				eventObj = displayList[displayIndex-1];
				//if (statusMessage!="")
					//statusMessage = currentList[dateIndex].getEventDetails(indexToEdit);
				
				commandOrder.push("edit");
				commandOrder.push("edit");
				eventOrder.push(eventObj);
				currentList[dateIndex].editEvent(indexToEdit,currentTask,time[0],time[1]);
			}
		}
		else
			flag = false;
	}
	return flag;
}

bool ProcessList::showEvents (vector<Date>& currentList, vector<Event>& displayList ,struct Task currentTask)
{
	bool flag = true;
	Date date;
	if (currentTask.name!="")
	{
		displayList.clear();
		if (currentTask.name == "deadline")
		{
			for (int i =0; i <currentList.size();i++)
			{
				for (int j = 0; j<currentList[i].getEventListLength(); j++)
				{
					if (currentList[i].getEvent(j).getEventType()=="d")
						displayList.push_back(currentList.at(i).getEvent(j));
				}
			}
		}
		else
		{
			for (int i =0; i <currentList.size();i++)
			{
				for (int j = 0; j<currentList[i].getEventListLength(); j++)
				{
					if (hasString(currentTask.name, currentList[i].getEvent(j).getName()))
						displayList.push_back(currentList.at(i).getEvent(j));
				}
			}
		}
	}

	else if (currentTask.date!="")
	{
		int index;
		Date currentDate;
		currentDate=currentList[0];
		Date dateToDisplay=currentDate.dateStringToObject(currentTask.date);
		
		if(currentDate.isValidDate(dateToDisplay))
		{
			 index = currentList[0].dateDifference(dateToDisplay);
			 if (index>=currentList.size())
				 flag=false;
			 else
			 {
				 displayList.clear();
				 displayList = currentList[index].getEventList();
			 }
		}
		else
			flag = false;	
	}

	else if (currentTask.location!="")
	{
		displayList.clear();
		for (int i =0; i <currentList.size();i++)
		{
			for (int j = 0; j<currentList[i].getEventList().size(); j++)
			{
				if (hasString(currentTask.location, currentList[i].getEvent(j).getLocation()))
					displayList.push_back(currentList[i].getEvent(j));
			}
		}
	}
	if (!flag)
		displayList.clear();

	return flag;

}

bool ProcessList::hasString(string userInput, string stringToBeSearched)
{
	int found;
	found=stringToBeSearched.find(userInput);
	if(found!=string::npos)
		return true;
	return false;
}

string ProcessList:: undoCommand (vector<Date>& currentList)
{
	if (commandOrder.size()==0)
		return "";
	else
	{
		string commandType=commandOrder.top();
		commandOrder.pop();
		Event eventObj=eventOrder.top();
		eventOrder.pop();

		struct Task currentTask;
		if(commandType=="done")
		{
			Date dateToUnmark=currentList[0].dateStringToObject( eventObj.getId().substr(0,8));
			int index = currentList[0].dateDifference(dateToUnmark);
			currentList[index].markEventAsNotDone(currentList[index].getIndex(eventObj.getId()));
			return commandType;
		}
		if (commandType=="delete")
		{
			Date dateToAdd=currentList[0].dateStringToObject( eventObj.getId().substr(0,8));
			int index = currentList[0].dateDifference(dateToAdd);
			currentList[index].addEvent(eventObj.getId(),eventObj.getName(),eventObj.getTime(), eventObj.getLocation(), eventObj.getStartTime(), eventObj.getEndTime(), eventObj.getEventType(), eventObj.getEventStatus());
			return commandType;
		}
		else if (commandType=="edit")
		{
			Event eventObj2=eventOrder.top();
			eventOrder.pop();
			commandOrder.pop();
			Date dateToEdit=currentList[0].dateStringToObject( eventObj2.getId().substr(0,8));
			int dateIndex = currentList[0].dateDifference(dateToEdit);

			int indexToDelete=currentList[dateIndex].getIndex(eventObj2.getId());//currentList[dateIndex].getIndex(id);
			currentList[dateIndex].deleteEvent(indexToDelete);
			
			dateToEdit=currentList[0].dateStringToObject( eventObj.getId().substr(0,8));
			dateIndex = currentList[0].dateDifference(dateToEdit);			
			currentList[dateIndex].addEvent(eventObj.getId(),eventObj.getName(),eventObj.getTime(), eventObj.getLocation(), eventObj.getStartTime(), eventObj.getEndTime(), eventObj.getEventType(), eventObj.getEventStatus());
			return commandType;
		}
		else if (commandType =="add")
		{
			Date dateToDelete=currentList[0].dateStringToObject( eventObj.getId().substr(0,8));
			int dateIndex = currentList[0].dateDifference(dateToDelete);
			int indexToDelete=currentList[dateIndex].getIndex(eventObj.getId());//currentList[dateIndex].getIndex(id);

			currentList[dateIndex].deleteEvent(indexToDelete);
			return commandType;

		}
	}
}

int ProcessList::getTimeArrayIndex(Time eventTime)
{
	int index;
	if(eventTime.getAmPm()=="")
	{
		return -1;
	}

	if(equalsIgnoreCase(eventTime.getAmPm(),"am"))
	{
		index=eventTime.getHour();
		if(index==12)
			index=0;
	}
	else
	{
		index=eventTime.getHour()+12;
		if(index==24)
			index=12;
	}
	return index;
}

string ProcessList::showFreeTime (vector<Date>& currentList, vector<Event>& displayList,struct Task currentTask)
{
	bool flag = true;
	bool hasDeadlines = false;
	Date date;
	int i,k;
	vector<Event> eventList;
	stringstream output;
	vector<Event>* timeArray[24];
	int start,end;

	for(int j=0;j<24;j++)
		timeArray[j]=new vector<Event>;
	
	if (currentTask.date!="")
	{
		int index,arrayIndex;
		Date currentDate;
		currentDate=currentList[0];
		Date dateToDisplay=currentDate.dateStringToObject(currentTask.date);
		
		if(currentDate.isValidDate(dateToDisplay))
		{
			index = currentDate.dateDifference(dateToDisplay);
			if(index>currentList.size()-1)
				output << "No events scheduled. You are free for the entire day";
			else
			{
				eventList=currentList[index].getEventList();
				if(eventList.size()==0)
					output << "No events scheduled. You are free for the entire day";
				else
				{
					for(int j=0;j<eventList.size();j++)
					{
						if (eventList[j].getEventType()!="d")
						{
							arrayIndex=getTimeArrayIndex(eventList[j].getEndTime());
							if (arrayIndex==-1)
								timeArray[getTimeArrayIndex(eventList[j].getStartTime())]->push_back(eventList[j]);
							if(arrayIndex==0)
								arrayIndex=24;
							for(k=getTimeArrayIndex(eventList[j].getStartTime());k<arrayIndex;k++)
							{
								timeArray[k]->push_back(eventList[j]);
							}
							if(eventList[j].getEndTime().getMinutes()!=0)
								timeArray[k]->push_back(eventList[j]);
						}
						else
							hasDeadlines = true;
					}
					
					start=7;
					end=7;
					for(i=7;i<24;i++)
					{
						if(timeArray[i]->size()==0)
							end++;
						
						else
						{
							if(start!=end)
							{
								output << "You are free from " << arrayIndexToString(start) << " to " << arrayIndexToString(end) << "\n";
								start=end;
							}
							start++;
							end++;
							
							//count++;
							
							/*Event temp=timeArray[i]->at(0);
							for(int j=1;j<timeArray[i]->size();j++)
							{
								Event current= timeArray[i]->at(j);
								if(!temp.getEndTime().isGreaterThan(current.getEndTime()))
									temp=current;
							}
								
							
							if(temp.getEndTime().getAmPm()!="")
							{
								arrayIndex=getTimeArrayIndex(temp.getEndTime());
								if(temp.getEndTime().getMinutes()!=0)
									arrayIndex+=1;
							}
							else
								arrayIndex=getTimeArrayIndex(temp.getStartTime())+1;
							start=arrayIndex;
							end=arrayIndex;
							if(arrayIndex==24)
								arrayIndex=0;
							i=arrayIndex-1;
						}*/
						//if(i==-1)
							//break;

						}
					}
					if(start!=end)
						output << "You are free from " << arrayIndexToString(start) << " to " << arrayIndexToString(end) << "\n";

					if (hasDeadlines)
					{
						output << "Please Note that you have the following Deadlines on the same day:\n";
						int count = 0;
						
						for(int j=0;j<eventList.size();j++)
						{
							if (eventList[j].getEventType()=="d")
							{
								count++;
								output << count << ". " << eventList[j].getName() << " by " << eventList[j].getTime() << "\n";
							}
						}
					}
				}
			}
		}
		else
			output << "Wrong Entry";	
	}
	displayList.clear();
	return output.str();
	
}

string ProcessList::arrayIndexToString(int index)
{
	Time time;
	Time tempTime;
	if(index==24)
		index=0;
	if(index<12)
	{
		if(index==0)
			time.setHour(12);
		else
			time.setHour(index);
		time.setAmPm("am");
	}
	else
	{
		if(index!=12)
			time.setHour(index-12);
		else
			time.setHour(12);
		time.setAmPm("pm");
	}
	return time.formatTime();
}

bool ProcessList::markAsDone (vector<Date>& currentList, vector<Event>& displayList, struct Task currentTask)
{
	bool flag=true;
	int displayIndex, i;
	Event eventObj;
	
	displayIndex=atoi(currentTask.index.c_str());
	if(displayIndex<1||displayIndex>displayList.size())
	{
		flag=false;
	}

	else
	{
		string id = displayList[displayIndex-1].getId();
		string date=id.substr(0,8);
		Date dateObj=currentList[0].dateStringToObject(date);
		int dateIndex=currentList[0].dateDifference(dateObj);
		int indexToMark=currentList[dateIndex].getIndex(id);
				
		if (indexToMark!=-1)
		{
			eventObj=currentList[dateIndex].getEvent(indexToMark);
			currentList[dateIndex].markEventAsDone(indexToMark);
			//statusMessage = currentList[dateIndex].getEventDetails(indexToDelete);
			commandOrder.push("done");
			eventOrder.push(eventObj);
		}
		else
			flag=false;
	}
	return flag;
}

ProcessList::~ProcessList(void)
{
}